package App.Model;

import javax.swing.*;
import java.util.Arrays;
import java.util.HashMap;

//OO主界面的用户列表拓展功能
public class UserListModel extends DefaultListModel {
    //    String[] s = {"userNull","userNull1","userNull","userNull1","userNull","userNull1","userNull","userNull1","userNull","userNull1","userNull","userNull1"};
    String[] s = {"userNull"};

    public int getSize() {
        return s.length;
    }

    public String getElementAt(int index) {//自动调用,对每个位置的值进行检验并输出指定结果
        return s[index++];
    }

    public void updateList(String data) {
        System.out.println("尝试更新用户数据");
        String[] two = data.split("\\|");//前者为所用用户,后者为在线用户
        String[] allUser = two[0].split(" ");
        String[] nowUser = two[1].split(" ");

        HashMap<String, Boolean> tempMap = new HashMap<>();
        for (String user : nowUser) {
            tempMap.put(user, true);
        }
        for (int i = 0; i < allUser.length; i++) {
            String user = allUser[i];
            if (tempMap.containsKey(user)) {
                allUser[i] = "●" + user;
            }
        }
        s = allUser;
        setSize(s.length);//大坑,size不会更新,需要手动更新
        System.out.println("用户数据更新完成" + Arrays.toString(allUser));
    }

    public void confirmThisUser(String userId) {
        //该用户前方的"●"进行闪烁更新
        for (int i = 0; i < s.length; i++) {
            if (s[i].equals(userId)) {
                s[i] = "●" + s[i];
            }
        }
    }

    public boolean isOnline(String userId){
        //判断该用户是否在线
        userId = "●" + userId;
        for (String eachS : s) {
            if(eachS.equals(userId))
                return true;
        }
        return false;
    }
}
